package com.group3.admin.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group3.admin.service.IAMerchantService;
import com.group3.admin.service.impl.AMerchantServiceImpl;
import com.group3.admin.util.JsonUtil;
import com.group3.entity.AMerchant;

/**
 * Servlet implementation class TypeServlet
 */
public class TypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IAMerchantService service = new AMerchantServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TypeServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		getType(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		getType(request, response);
	}

	private void getType(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String mId = request.getParameter("mId");
		OutputStream os = response.getOutputStream();;
		List<String> list = new ArrayList<>();
		System.out.println("get the mId from merchant is "+mId);
		AMerchant merchant = service.queryAMerchantByMId(mId);
		if(merchant != null){
			System.out.println("merchant get it's merchant from admin success ");
//			System.out.println("get the merchant is " + merchant);
			
			list.add(merchant.getType());
			list.add(merchant.getStatus());
			String listToJson = JsonUtil.getInstance().ObjectToJson(list);
			System.out.println("type is " +merchant.getType());
			os.write(listToJson.getBytes());
		}
		else {
			os.write("response from admin: sorry, no such m_Id".getBytes());
		}


	}

}
